package application.core;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextField;

import application.game.Level;
import application.utils.Convertor;
import application.utils.Loader;

public class Menu extends JMenuBar{
	private static final long serialVersionUID = 1L;
	private Application application;
	
	private JMenu fileMenu;
	private JMenuItem newAction;
	private JMenuItem openAction;
	private JMenuItem saveAction;
	private JMenuItem saveAsAction;
	private JMenuItem exitAction;
	
	private JMenu editMenu;
	private JMenuItem cutAction;
	private JMenuItem copyAction;
	private JMenuItem pasteAction;
	
	public Menu(Application application){
		super();
		this.application = application;
		
		setVisible(true);
		
		createFileMenu();
		createEditMenu();
	}
	
	private void createFileMenu(){
		fileMenu = new JMenu("File");
		
		newAction = new JMenuItem("New");
		openAction = new JMenuItem("Open");
		saveAction = new JMenuItem("Save");
		saveAsAction = new JMenuItem("Save as");
		exitAction = new JMenuItem("Exit");
		
		newAction.addActionListener(new ActionListener(){
			void createNewInputDialog(){
				final JDialog dialog = new JDialog();
				
				dialog.setVisible(true);
				dialog.setTitle("Level info");
				dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				dialog.setVisible(true);
				dialog.setLayout(null);
				dialog.setResizable(false);
				
				int initialWidth = 400;
				int initialHeight = 250;
				Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
				dialog.setBounds((int)(screenSize.getWidth() / 2 - initialWidth / 2), (int)(screenSize.getHeight() / 2 - initialHeight / 2), initialWidth, initialHeight);
				
				// name
				final JLabel nameLevelLabel = new JLabel("Level Name:");
				nameLevelLabel.setVisible(true);
				nameLevelLabel.setBounds(20, 25, 80, 25);
				dialog.add(nameLevelLabel);
				
				final JTextField nameLevelTextField = new JTextField();
				nameLevelTextField.setVisible(true);
				nameLevelTextField.setBounds(120, 25, 240, 25);
				dialog.add(nameLevelTextField);
				
				// frame width
				final JLabel frameWidthLabel = new JLabel("Frame Width:");
				frameWidthLabel.setVisible(true);
				frameWidthLabel.setBounds(20, 75, 80, 25);
				dialog.add(frameWidthLabel);
				
				final JTextField frameWidthTextField = new JTextField();
				frameWidthTextField.setVisible(true);
				frameWidthTextField.setBounds(120, 75, 80, 25);
				dialog.add(frameWidthTextField);
				
				// frame height
				final JLabel frameHeightLabel = new JLabel("Frame Height:");
				frameHeightLabel.setVisible(true);
				frameHeightLabel.setBounds(20, 125, 80, 25);
				dialog.add(frameHeightLabel);
				
				final JTextField frameHeightTextField = new JTextField();
				frameHeightTextField.setVisible(true);
				frameHeightTextField.setBounds(120, 125, 80, 25);
				dialog.add(frameHeightTextField);
				
				// image path
				final JLabel imagePathLabel = new JLabel("Image Path:");
				imagePathLabel.setVisible(true);
				imagePathLabel.setBounds(20, 175, 80, 25);
				dialog.add(imagePathLabel);
				
				final JTextField imagePathTextField = new JTextField();
				imagePathTextField.setVisible(true);
				imagePathTextField.setBounds(120, 175, 220, 25);
				dialog.add(imagePathTextField);
				
				final JButton imagePathButton = new JButton("...");
				imagePathButton.setVisible(true);
				imagePathButton.setBounds(340, 175, 20, 25);
				imagePathButton.addActionListener(new ActionListener(){
					@Override
					public void actionPerformed(ActionEvent e){
						JFileChooser fileChooser = new JFileChooser(".");
						int status = fileChooser.showOpenDialog(null);
						if(status == JFileChooser.APPROVE_OPTION){
							imagePathTextField.setText(fileChooser.getSelectedFile().getPath());
						}
					}
				});
				dialog.add(imagePathButton);
				
				// ok
				final JButton okButton = new JButton("Ok");
				okButton.setVisible(true);
				okButton.setBounds(240, 75, 80, 25);
				okButton.addActionListener(new ActionListener(){
					@Override
					public void actionPerformed(ActionEvent e){
						String name = nameLevelTextField.getText();
						int rows = Convertor.getInt(frameHeightTextField.getText());
						int columns = Convertor.getInt(frameWidthTextField.getText());
						String imagePath = imagePathTextField.getText();
						if(name.isEmpty() == false && rows > 0 && columns > 0 && imagePath.isEmpty() == false && application != null && application.mapArea != null && application.project != null){
							application.mapArea.bufferedImage = Loader.loadImage(imagePath);
							application.mapArea.repaint();
							
							application.project.level = new Level(name, rows, columns);
							dialog.dispose();
						}
					}
				});
				dialog.add(okButton);
				
				// cancel
				final JButton cancelButton = new JButton("Cancel");
				cancelButton.setVisible(true);
				cancelButton.setBounds(240, 125, 80, 25);
				cancelButton.addActionListener(new ActionListener(){
					@Override
					public void actionPerformed(ActionEvent e){
						dialog.dispose();
					}
				});
				dialog.add(cancelButton);
			}
			
			public void actionPerformed(ActionEvent event){
				createNewInputDialog();
			}
		});
		openAction.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				System.out.println("Open");
			}
		});
		saveAction.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				System.out.println("Save");
			}
		});
		saveAsAction.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				if(application != null && application.project != null){
					application.project.saveAs();
				}
			}
		});
		exitAction.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				System.out.println("Exit");
			}
		});
		
		fileMenu.add(newAction);
		fileMenu.add(openAction);
		fileMenu.addSeparator();
		fileMenu.add(saveAction);
		fileMenu.add(saveAsAction);
		fileMenu.addSeparator();
		fileMenu.add(exitAction);
		
		add(fileMenu);
	}
	
	private void createEditMenu(){
		editMenu = new JMenu("Edit");
		
		cutAction = new JMenuItem("Cut");
		copyAction = new JMenuItem("Copy");
		pasteAction = new JMenuItem("Paste");
		
		cutAction.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				System.out.println("Cut");
			}
		});
		copyAction.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				System.out.println("Copy");
			}
		});
		pasteAction.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				System.out.println("Paste");
			}
		});
		
		editMenu.add(cutAction);
		editMenu.add(copyAction);
		editMenu.add(pasteAction);
		
		add(editMenu);
	}
}
